<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-03-20
 * Time: 17:30:15
 * Info:
 */

namespace app\index\controller;

use think\App;
use think\exception\ValidateException;
use think\Validate;
use think\facade\Cache;
use app\common\model\Config as ConfigModel;

abstract class BaseController
{

    use \app\common\traits\JumpTrait;

    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 视图
     * @var \think\View | \think\Template
     */
    protected $view;

    /**
     * 配置
     * @var \think\Config
     */
    protected $config;

    /**
     * 缓存
     * @var \think\Cache
     */
    protected $cache;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 构造方法
     * @access public
     *
     * @param App $app 应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;
        $this->view    = $this->app->view;
        $this->config  = $this->app->config;
        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {
        if ($this->app->isDebug()) {
            // todo:在调试模式下，需要关闭的功能

        }

        //初始化配置
        $site = [
            'root_domain' => $this->request->baseFile(true), // 域名地址
            'root_file'   => trim($this->request->baseFile(), '/'), // 入口文件
        ];
        //临时关闭站点
        $close_page = get_config('close_site');
        if (empty($close_page)) {
            $this->closePage($site);
        }

        // todo:加载当前控制器语言包

        // todo:加载模板语言包

        // todo:多语言内容配置

    }

    /**
     * 站点关闭页
     *
     * @param $site
     */
    private function closePage($site)
    {
        $html = <<<EOF
<!DOCTYPE html>
<html lang="zh">
<head>
    
    <link rel="Shortcut Icon" href="" type="image/x-icon" />
    <style>html,body {width: 100%;height: 100%;margin: 0}div {font-size: 28px;padding-top: 200px;}</style>
</head>
<body><div><p align="center">站点临时关闭维护中...</p></div></body>
</html>
EOF;
        throw new \think\exception\HttpResponseException(display($html, ['site' => $site]));

    }

}
